/*list magic by Arthur Wulf White
jquery,  jquery ui and jeditable required
*/
function makeEditable()
{
	$('.editable').editable(function(value, settings)
	{ 				
		console.log(this);
		console.log(value);
		console.log(settings);
		return(value);
	},
	{
		onblur : 'submit'
	}
	);
}
function makeDelable()
{
	$('a.delete').click(function(e) 
	{
		e.preventDefault();
		$(this).parent().remove();
	});
}
function addTopic()
{
	var topicName = $('input[name=newListItem]').val();
	$('input[name=newListItem]').val('');
	if(topicName == '')
		return;
	$("#magicList").append('<li><img class="lefticon" src="img/vertical_movement_sign2.png"/><span class="editable">' + topicName +'</span><a class="delete" href=""> </a></li>');
	makeEditable();
	makeDelable();
}

$(document).ready(function()
{
	$('#topic').keydown(
    function(event)
    {
        if($(this).val().length >= 44)
        {
            $(this).val($(this).val().substring(0, 45));
        }
    });
	$("form").submit(function() {
		addTopic();
		return false;
	});
	$('a#add').click(function(e)
	{
		e.preventDefault();
		addTopic();
	});
	$('a#update').click(function(e)
	{
		e.preventDefault();
		var data = {};
		$('#magicList .editable').each(function(i) {
			data['topicname' + (i + 1)] = $(this).text();
		});
		//$.post("setClassLayoutData", $.param(data));
		console.log($.param(data));
	});
	makeEditable();
	makeDelable();
	$( "#magicList" ).sortable();
});
